package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.Comment;
import com.example.jyjava.dao.CommentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentDao commentDao;

    @RequestMapping("/addComment")
    public String addComment(@RequestBody Comment comment){
        String falg = "400";
        comment.setCreatTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setDeleteType(false);
        int i = commentDao.addComment(comment);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("collectNum",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getComment")
    public String getComment(@RequestBody Comment comment){
        String falg = "400";
        List<Comment> commentList = commentDao.getComment(comment.getShopId());
        HashMap<String, Object> res = new HashMap<>();
        if (commentList != null){
            falg = "200";
            res.put("flag",falg);
            res.put("commentList",commentList);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getCommentSin")
    public String getCommentSin(@RequestBody Comment comment){
        String falg = "400";
        Comment co = commentDao.getCommentSin(comment.getShopId(),comment.getUserId(),comment.getOrderId());
        HashMap<String, Object> res = new HashMap<>();
        if (co != null){
            falg = "200";
            res.put("flag",falg);
            res.put("comment",co);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

}
